package cn.piggy.gateway.sdk.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;


@ConditionalOnProperty(
        prefix = "api-application-sdk",
        name = "enabled",
        havingValue = "true",
        matchIfMissing = true
)
@EnableConfigurationProperties(GatewaySDKServiceProperties.class)
public class GatewaySDKAutoConfig {

    @Bean(initMethod = "start")
    public CuratorFramework curatorFramework(GatewaySDKServiceProperties properties) {
        String systemRegistry = properties.getSystemRegistry();
        String substring = systemRegistry.substring(12);
        String[] split = substring.split("\\?");
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);
        return CuratorFrameworkFactory.builder()
                .connectString(split[0]).retryPolicy(retryPolicy)
                .build();
    }
}
